<template>
  <div class="login">
    <!-- {{hidden}} -->
    <!-- {{userinfo}} -->
    <div class="alert-top" v-if="off">
      <a-alert message="登入成功" type="success" :banner="true" />
    </div>
    <div class="img">
      <Tabimg :style="hidden <= 1000 ? { display: 'none' } : ''" :listimg="listimg" />
    </div>
    <div class="login-box">
      <h1>未来社区Login</h1>
      <h5>每一天乐在沟通</h5>

      <b-form @submit="onSubmit" @reset="onReset" v-if="show">
        <b-form-group class="mt-3" id="input-group-1" label="手机" label-for="input-1">
          <b-form-input
            :class="verify.class"
            class="mt-1"
            id="input-1"
            v-model="form.iphone"
            type="text"
            placeholder="请输入手机号"
            required
          ></b-form-input>
        </b-form-group>
        <b-form-group
          class="mt-3"
          id="input-group-3"
          label="登入密码"
          label-for="input-3"
        >
          <b-form-input
            :class="passwordverify.class"
            class="mt-1"
            id="input-3"
            type="password"
            v-model="form.password"
            placeholder="请输入密码"
            required
          ></b-form-input>
        </b-form-group>
        <b-row>
          <b-button
            type="submit"
            variant="primary"
            class="mt-4 btn btn-primary btn-md btn_width"
            >登入</b-button
          >
          <router-link class="btn-a" :to="{ name: 'Register' }"
            >没有账号？去注册入</router-link
          >
        </b-row>
      </b-form>
    </div>
  </div>
</template>

<script>
import Tabimg from "../components/Tabimg.vue";
export default {
  data() {
    return {
      form: {
        iphone: "",
        name: "",
        password: "",
        password2: "",
        code: "",
        food: null,
        checked: [],
        imgoff: false,
        index: 0,
      },
      foods: [
        { text: "Select One", value: null },
        "Carrots",
        "Beans",
        "Tomatoes",
        "Corn",
      ],
      show: true,
      listimg: [
        require("@/assets/01-1.jpg"),
        require("@/assets/01-2.jpg"),
        require("@/assets/01-3.jpg"),
        require("@/assets/01-4.jpg"),
      ],
      off:false
    };
  },
  components: {
    Tabimg,
  },
  methods: {
    async onSubmit(event) {
      event.preventDefault();
      let res = await this.$Api.login(this.form);

      // console.log(res.data.ret);
      //登入成功后才会做的事情 要写if判断
      if (res.data.ret == 200) {
        this.off=true
        localStorage.setItem("userinfo", JSON.stringify(res.data.data));
        setTimeout(() => {
          this.$router.push({ name: "Home" });
        }, 500);

        // this.VerifyTkoen();
      }
    },
    onReset(event) {
      event.preventDefault();
      this.form.email = "";
      this.form.name = "";
      this.form.food = null;
      this.form.checked = [];
      this.show = false;
      this.$nextTick(() => {
        this.show = true;
      });
    },
    async VerifyTkoen() {
      let params = {
        phone: this.form.iphone,
        token: localStorage.getItem("wlsqtoken"),
      };
      let res = await this.$Api.verifytoken(params);
      console.log(res);
    },
  },
  computed: {
    hidden() {
      let hidden = document.documentElement.offsetWidth;
      return hidden;
    },
    verify() {
      let myreg = /^[1][3,4,5,6.7,8,9][0-9]{9}$/; //手机号码正则
      return this.$Code.verify(this.form.iphone, myreg);
    },
    passwordverify() {
      let patten = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
      return this.$Code.verify(this.form.password, patten);
    },
  },
};
</script>

<style lang="scss">
.login {
  width: 100vw;
  display: flex;
  flex-wrap: wrap;
  .login-box {
    width: 25%;
    min-width: 350px;
    margin: auto;
  }
  .img {
    img {
      height: 100vh;
    }
  }
  .btn-a {
    text-decoration: none;
    width: 100%;
    text-align: center;
  }
  .btn_width {
    margin: auto;
    width: 90%;
  }
  .alert-top{
    width: 300px;
    position: absolute;
    top: 0px;
    left: 50%;
    z-index: 999;

  }
}
</style>
